package com.sch.www.oauth.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

@RestController
public class Oauth2ExtendController {

    @Autowired
    private TokenStore tokenStore;


    /**
     * 移除 token
     *
     * @param value
     * @return
     */
    @RequestMapping(value = "/oauth/removeToken")
    @ResponseBody
    public Map<String, Object> removeToken(Principal principal) {






        OAuth2AccessToken token = tokenStore.readAccessToken(null);
        if (token == null) {
            throw new InvalidTokenException("Token was not recognised");
        } else {
            tokenStore.removeAccessToken(token);
            Map<String, Object> result = new HashMap<>();
            result.put("result", "success");
            return result;
        }

    }


}
